package cn.stylefeng.guns.sys.modular.system.controller;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.auth.model.LoginUser;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.system.entity.Dept;
import cn.stylefeng.guns.sys.modular.system.entity.DingdingDayAttend;
import cn.stylefeng.guns.sys.modular.system.model.params.*;
import cn.stylefeng.guns.sys.modular.system.model.result.DingdingEmpResult;
import cn.stylefeng.guns.sys.modular.system.model.result.DingdingEmpSimpleResult;
import cn.stylefeng.guns.sys.modular.system.service.DingdingDayAttendService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;


/**
 * 钉钉，前三十天，人员出勤数据控制器
 *
 * @author zxd
 * @Date 2022-03-08 15:08:28
 */
@Controller
@RequestMapping("/dingdingDayAttend")
public class DingdingDayAttendController extends BaseController {

    private String PREFIX = "/modular/system/dingdingDayAttend";

    @Autowired
    private DingdingDayAttendService dingdingDayAttendService;

    /**
     * 跳转到主页面
     *
     * @author zxd
     * @Date 2022-03-08
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/dingdingDayAttend.html";
    }

    /**
     * 新增页面
     *
     * @author zxd
     * @Date 2022-03-08
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/dingdingDayAttend_add.html";
    }

    /**
     * 编辑页面
     *
     * @author zxd
     * @Date 2022-03-08
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/dingdingDayAttend_edit.html";
    }

    /**
     * 新增接口
     *
     * @author zxd
     * @Date 2022-03-08
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(DingdingDayAttendParam dingdingDayAttendParam) {
        this.dingdingDayAttendService.add(dingdingDayAttendParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author zxd
     * @Date 2022-03-08
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(DingdingDayAttendParam dingdingDayAttendParam) {
        this.dingdingDayAttendService.update(dingdingDayAttendParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author zxd
     * @Date 2022-03-08
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(DingdingDayAttendParam dingdingDayAttendParam) {
        this.dingdingDayAttendService.delete(dingdingDayAttendParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author zxd
     * @Date 2022-03-08
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(DingdingDayAttendParam dingdingDayAttendParam) {
        DingdingDayAttend detail = this.dingdingDayAttendService.getById(dingdingDayAttendParam.getId());
        return ResponseData.success(detail);
    }


    /**
     * 获取钉钉人员，部门，打卡，关联关系表
     *
     * @author wjk
     * @Date 2022-03-11
     */
    @ResponseBody
    @PostMapping("/empAndDeptAndDingdingAttendList")
    public LayuiPageInfo empAndDeptAndDingdingAttendList(EmpAndDeptAndDingdingAttendParam empAndDeptAndDingdingAttendParam){
        LayuiPageInfo layuiPageInfo = this.dingdingDayAttendService.empAndDeptAndDingdingAttendList(empAndDeptAndDingdingAttendParam);
        return layuiPageInfo;
    }


    /**
     *  导出excel表格
     *  获取钉钉人员，部门，打卡，关联关系表
     * @author wjk
     * @Date 2021-03-21
     */
    @ResponseBody
    @RequestMapping("/exportEmpAndDeptAndDingdingAttendList")
    public ResponseEntity<byte[]> exportEmpAndDeptAndDingdingAttendList(HttpServletRequest request, HttpServletResponse response, EmpAndDeptAndDingdingAttendParam empAndDeptAndDingdingAttendParam) {
        return this.dingdingDayAttendService.exportEmpAndDeptAndDingdingAttendList(request, response, empAndDeptAndDingdingAttendParam);
    }



    /**
     *  导出excel表格
     *  获取钉钉人员，部门，打卡，关联关系表
     *  根据大级别的部门，获取小级别的部门，并且将小部门分组；
     * @author wjk
     * @Date 2021-03-22
     */
    @ResponseBody
    @RequestMapping("/exportEmpAndDeptArrayList")
    public ResponseEntity<byte[]> exportEmpAndDeptArrayList(HttpServletRequest request, HttpServletResponse response, EmpAndDeptAndDingdingAttendParam empAndDeptAndDingdingAttendParam) {
        return this.dingdingDayAttendService.exportEmpAndDeptArrayList(request, response, empAndDeptAndDingdingAttendParam);
    }


    /**
     *  根据钉钉编号，查询近二十天，关联的表单信息
     *
     * @author wjk
     * @Date 2022-03-18
     */
    @RequestMapping("/queryRequestDetailByBh")
    @ResponseBody
    public ResponseData queryRequestDetailByBh(EmpAndDeptAndDingdingAttendParam empAndDeptAndDingdingAttendParam){
        List<DingdingEmpSimpleResult> list = this.dingdingDayAttendService.queryRequestDetailByBh(empAndDeptAndDingdingAttendParam);
        return ResponseData.success(list);
    }






    /**
     * 删除钉钉人员数据，根据人员在部门钉钉里面的编号
     * 并且将人员标记为离职状态
     * 将该人员的打卡记录标记为不打卡
     *
     * @author wjk
     * @Date 2022-03-16
     */
    @ResponseBody
    @PostMapping("/deleteDingdingEmpByBhList")
    public ResponseData deleteDingdingEmpByBhList(EmpAndDeptAndDingdingAttendParam empAndDeptAndDingdingAttendParam){
        List<String> list = empAndDeptAndDingdingAttendParam.getIdList();

        if(ToolUtil.isEmpty(list)||list.size() == 0 ){
            return ResponseData.error(401,"参数不正确，请确认后进行操作");
        }

        //获取当前用户角色列表
        LoginUser user = LoginContextHolder.getContext().getUser();
        if(!String.valueOf(user.getId()).equals("1269182893637537794")){
            return ResponseData.error(401,"权限不正确，请确认，当前登录人是否有操作权限，当前有操作权限仅限人事部。");
        }


        Map<String,String> map = this.dingdingDayAttendService.deleteDingdingEmpByBhList(list);

        if(map.get("flag").equals("true")){
            return ResponseData.success(map);
        }
        if(map.get("flag").equals("false")){
            return ResponseData.error(401,map.get("msg"));
        }
         return ResponseData.error(401,"权限不正确，请确认，当前登录人是否有操作权限，当前有操作权限仅限人事部。");
    }

    /**
     * 当前登录人是否有删除人员的权限。
     *
     * @author wjk
     * @Date 2022-03-18
     */
    @ResponseBody
    @PostMapping("/deleteDingdingEmpByBhListPermissions")
    public ResponseData deleteDingdingEmpByBhListPermissions(){
        //获取当前用户角色列表
        LoginUser user = LoginContextHolder.getContext().getUser();
        if(String.valueOf(user.getId()).equals("1269182893637537794")){
            return ResponseData.success(true);
        }

        return ResponseData.error(401,"权限不正确，请确认，当前登录人是否有操作权限，当前有操作权限仅限人事部。",false);

    }

    /**
     * 查询列表
     *
     * @author zxd
     * @Date 2022-03-08
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(DingdingDayAttendParam dingdingDayAttendParam) {
        return this.dingdingDayAttendService.findPageBySpec(dingdingDayAttendParam);
    }

    @RequestMapping("/queryDepts")
    @ResponseBody
    public List<Dept> queryDepts(Pid pid){
        return this.dingdingDayAttendService.queryDepts(pid);
    }

    /**
     * ws
     * 3.查询所有营运经理
     */
    @ResponseBody
    @PostMapping("/listYyjl")
    public LayuiPageInfo listYyjl(BtyPerformanceMonthInformationParam btyPerformanceMonthInformationParam){
        return this.dingdingDayAttendService.findPageYyjl(btyPerformanceMonthInformationParam);
    }

    /**
     * ws
     * 4.查询所有督导(可以根据营运经理电话)
     */
    @ResponseBody
    @PostMapping("/listDd")
    public LayuiPageInfo listDd(AttendAuditBtyAreaParam param){
        return this.dingdingDayAttendService.findPageDd(param);
    }

    /**
     * ws
     * 5.查询所有门店(可以根据营运经理电话，督导电话)
     */
    @ResponseBody
    @PostMapping("/listDm")
    public LayuiPageInfo listDm(AttendAuditBtyAreaParam param){
        return this.dingdingDayAttendService.findPageDm(param);
    }

    /**
     * ws
     * 6.员工表里的所有员工
     */
    @ResponseBody
    @PostMapping("/listXm")
    public LayuiPageInfo listXm(){
        return this.dingdingDayAttendService.findPageXm();
    }




}


